//
//  RecommendViewController.m
//  MusicPlayer
//
//  Created by dlios on 15-5-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RecommendViewController.h"
#import "RecommendTableViewCell.h"
#import "RecommendLatestModel.h"
#import "TouchImage.h"
#import "RecommendPostersScrollView.h"
#import "RecommendNewMusicViewController.h"
#import "ReNewAlbumViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "ReHotSongListViewController.h"
#import "WebViewController.h"
#import "Singleton.h"

@interface RecommendViewController ()<RecommendPostersDelegate, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, retain) UITableView *recommendTableView;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *posterArray;
@property (nonatomic, retain) RecommendPostersScrollView *scrollView;
@property (nonatomic, retain) NSMutableArray *sectionArray;
@property (nonatomic, retain) NSMutableArray *scrollArray;

@end

@implementation RecommendViewController
- (void)dealloc
{
    [_recommendTableView release];
    [_array release];
    [_posterArray release];
    [_scrollView release];
    [_sectionArray release];
    [_scrollArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.posterArray = [NSMutableArray array];
        self.sectionArray = [NSMutableArray array];
        self.scrollArray = [NSMutableArray array];
        
        
     
       
    }
    return self;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];

     self.view.backgroundColor = [UIColor clearColor];


    

//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//    [imageView setImage:[UIImage imageNamed:@"PIC2.png"]];
//    [self.view addSubview:imageView];


////    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
////    [imageView setImage:[UIImage imageNamed:@"PIC2.png"]];
//    [self.view addSubview:imageView];

    [self createRecommendTableView];
    [self createPostersScrollView];
    [self connect];
   
//    self.recommendTableView.tableHeaderView = _scrollView;
//    [self createPostersScrollView];
    

    
    
}




#pragma mark - 使用AFNetworking网络连接
- (void)connect
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *str = [NSString stringWithFormat:@"http://online.dongting.com/recomm/recomm_modules?v=v7.6.0.2014121818"];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        
        // 遍历出tableView所需要的信息
        NSArray *array = [dataDic objectForKey:@"songlists"];
        for (NSDictionary *dic in array) {
            NSArray *array2 = [dic objectForKey:@"data"];
            for (NSDictionary *dic2 in array2) {
                RecommendLatestModel *model = [[RecommendLatestModel alloc] init];
                [model setValuesForKeysWithDictionary:dic2];
                [self.array addObject:model];
            }
        }
        
        // 从songlists中找出需要的三个栏目
        NSDictionary *dic2 = [array objectAtIndex:0];
        [self.sectionArray addObject:dic2];
        NSDictionary *dic3 = [array objectAtIndex:1];
        [self.sectionArray addObject:dic3];
        NSDictionary *dic4 = [array lastObject];
        [self.sectionArray addObject:dic4];
        // 遍历出滚动条所需要的信息
        NSArray *array2 = [dataDic objectForKey:@"posters"];
        for (NSDictionary *dic in array2) {
            NSArray *array2 = [dic objectForKey:@"data"];
            for (NSDictionary *dic2 in array2) {
                RecommendLatestModel *model = [[RecommendLatestModel alloc] init];
                [model setValuesForKeysWithDictionary:dic2];
                [self.posterArray addObject:model];
                [self.scrollArray addObject:dic2];
            }
        }
        [self.scrollView setScrollViewIamage:_posterArray];
        [self.recommendTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

#pragma mark - 创建滚动的推荐
- (void)createPostersScrollView
{
    // 创建滚动的
    self.scrollView = [[RecommendPostersScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 667 * 200)];
    self.scrollView.delegate = self;
    
    
    self.scrollView.alpha = 0.9;
    [self.view addSubview:self.scrollView];
    [self.scrollView release];

}

- (void)imageTouch:(TouchImage *)image
{
    // 根据图片的tag值来确定点击事件
    if (image.tag == 2000) {
        NSString * string = [[self.scrollArray objectAtIndex:0] objectForKey:@"url"];
        // 判断两种情况，如果链接长度不为0，则为webView，以下判断同此
        if (string.length == 0) {
            NSDictionary *dic = [self.scrollArray objectAtIndex:0];
            NSNumber *num = [dic objectForKey:@"_id"];
            
            ReHotSongListViewController *hot = [[ReHotSongListViewController alloc] init];
            hot.str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[%@]", num];
            [self.navigationController pushViewController:hot animated:YES];
        } else
        {
            WebViewController * webVC = [[WebViewController alloc] init];
            webVC.webString = [[self.scrollArray objectAtIndex:2] objectForKey:@"url"];
            [self.navigationController pushViewController:webVC animated:YES];
            // 点击进入到播放界面后，移除底部播放状态条
            NSArray *arr = self.view.window.subviews;
            UIView *view = [arr objectAtIndex:1];
            view.alpha = 0;
            
            UIButton *button = [view.subviews objectAtIndex:0];
            [button setImage:[UIImage imageNamed:@"pause.ico"] forState:UIControlStateNormal];
            button.alpha = 0;
            
            UIButton *button1 = [view.subviews objectAtIndex:1];
            button1.alpha = 0;
            
            UIImageView *imageView = [view.subviews objectAtIndex:2];
            imageView.alpha = 0;
            
            
            UILabel *label = [view.subviews lastObject];
            label.alpha = 0;
            UIImageView *albumImageView = [view.subviews objectAtIndex:3];
            albumImageView.alpha = 0;
            
            Singleton *singleton = [Singleton shareManager];
            singleton.name = @"暂停";
            [singleton.audio pause];

            
        }
    }
    
    if (image.tag == 4000) {
        NSString * string = [[self.scrollArray objectAtIndex:1] objectForKey:@"url"];
        if (string.length == 0) {
            NSDictionary *dic = [self.scrollArray objectAtIndex:1];
            NSNumber *num = [dic objectForKey:@"_id"];
            
            ReHotSongListViewController *hot = [[ReHotSongListViewController alloc] init];
            hot.str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[%@]", num];
            [self.navigationController pushViewController:hot animated:YES];
        } else
        {
            WebViewController * webVC = [[WebViewController alloc] init];
            webVC.webString = [[self.scrollArray objectAtIndex:2] objectForKey:@"url"];
            [self.navigationController pushViewController:webVC animated:YES];
            // 点击进入到播放界面后，移除底部播放状态条
            NSArray *arr = self.view.window.subviews;
            UIView *view = [arr objectAtIndex:1];
            view.alpha = 0;
            
            UIButton *button = [view.subviews objectAtIndex:0];
            [button setImage:[UIImage imageNamed:@"pause.ico"] forState:UIControlStateNormal];
            button.alpha = 0;
            
            UIButton *button1 = [view.subviews objectAtIndex:1];
            button1.alpha = 0;
            
            UIImageView *imageView = [view.subviews objectAtIndex:2];
            imageView.alpha = 0;
            
            
            UILabel *label = [view.subviews lastObject];
            label.alpha = 0;
            UIImageView *albumImageView = [view.subviews objectAtIndex:3];
            albumImageView.alpha = 0;
            
            Singleton *singleton = [Singleton shareManager];
            singleton.name = @"暂停";
            [singleton.audio pause];
            [button setImage:[UIImage imageNamed:@"play.ico"] forState:UIControlStateNormal];
            
        }
    }
    if (image.tag == 6000) {
        NSString * string = [[self.scrollArray objectAtIndex:2] objectForKey:@"url"];
        if (string.length == 0) {
            NSDictionary *dic = [self.scrollArray objectAtIndex:2];
            NSNumber *num = [dic objectForKey:@"_id"];
            
            ReHotSongListViewController *hot = [[ReHotSongListViewController alloc] init];
            hot.str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[%@]", num];
            [self.navigationController pushViewController:hot animated:YES];
        } else
        {
            WebViewController * webVC = [[WebViewController alloc] init];
            webVC.webString = [[self.scrollArray objectAtIndex:2] objectForKey:@"url"];
            [self.navigationController pushViewController:webVC animated:YES];
            // 点击进入到播放界面后，移除底部播放状态条
            NSArray *arr = self.view.window.subviews;
            UIView *view = [arr objectAtIndex:1];
            view.alpha = 0;
            
            UIButton *button = [view.subviews objectAtIndex:0];
            [button setImage:[UIImage imageNamed:@"pause.ico"] forState:UIControlStateNormal];
            button.alpha = 0;
            
            UIButton *button1 = [view.subviews objectAtIndex:1];
            button1.alpha = 0;
            
            UIImageView *imageView = [view.subviews objectAtIndex:2];
            imageView.alpha = 0;
            
            
            UILabel *label = [view.subviews lastObject];
            label.alpha = 0;
            UIImageView *albumImageView = [view.subviews objectAtIndex:3];
            albumImageView.alpha = 0;
            
            Singleton *singleton = [Singleton shareManager];
            singleton.name = @"暂停";
            [singleton.audio pause];
            [button setImage:[UIImage imageNamed:@"play.ico"] forState:UIControlStateNormal];
            
        }
    }
    if (image.tag == 8000) {
        NSString * string = [[self.scrollArray objectAtIndex:3] objectForKey:@"url"];
        if (string.length == 0) {
            NSDictionary *dic = [self.scrollArray objectAtIndex:3];
            NSNumber *num = [dic objectForKey:@"_id"];
            
            ReHotSongListViewController *hot = [[ReHotSongListViewController alloc] init];
            hot.str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[%@]", num];
            [self.navigationController pushViewController:hot animated:YES];
        } else
        {
            WebViewController * webVC = [[WebViewController alloc] init];
            webVC.webString = [[self.scrollArray objectAtIndex:3] objectForKey:@"url"];
            [self.navigationController pushViewController:webVC animated:YES];
            // 点击进入到播放界面后，移除底部播放状态条
            NSArray *arr = self.view.window.subviews;
            UIView *view = [arr objectAtIndex:1];
            view.alpha = 0;
            UIButton *button = [view.subviews objectAtIndex:0];
            [button setImage:[UIImage imageNamed:@"pause.ico"] forState:UIControlStateNormal];
            button.alpha = 0;
            
            UIButton *button1 = [view.subviews objectAtIndex:1];
            button1.alpha = 0;
            
            UIImageView *imageView = [view.subviews objectAtIndex:2];
            imageView.alpha = 0;
            
            
            UILabel *label = [view.subviews lastObject];
            label.alpha = 0;
            UIImageView *albumImageView = [view.subviews objectAtIndex:3];
            albumImageView.alpha = 0;
            
            Singleton *singleton = [Singleton shareManager];
            singleton.name = @"暂停";
            [singleton.audio pause];
            [button setImage:[UIImage imageNamed:@"play.ico"] forState:UIControlStateNormal];
            
        }
    }
    if (image.tag == 10000) {
        NSString * string = [[self.scrollArray objectAtIndex:4] objectForKey:@"url"];
        if (string.length == 0) {
            NSDictionary *dic = [self.scrollArray objectAtIndex:4];
            NSNumber *num = [dic objectForKey:@"_id"];
            
            ReHotSongListViewController *hot = [[ReHotSongListViewController alloc] init];
            hot.str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[%@]", num];
            [self.navigationController pushViewController:hot animated:YES];
        } else
        {
            WebViewController * webVC = [[WebViewController alloc] init];
            webVC.webString = [[self.scrollArray objectAtIndex:4] objectForKey:@"url"];
            [self.navigationController pushViewController:webVC animated:YES];
            // 点击进入到播放界面后，移除底部播放状态条
            NSArray *arr = self.view.window.subviews;
            UIView *view = [arr objectAtIndex:1];
            view.alpha = 0;
            
            UIButton *button = [view.subviews objectAtIndex:0];
            [button setImage:[UIImage imageNamed:@"pause.ico"] forState:UIControlStateNormal];
            button.alpha = 0;
            
            UIButton *button1 = [view.subviews objectAtIndex:1];
            button1.alpha = 0;
            
            UIImageView *imageView = [view.subviews objectAtIndex:2];
            imageView.alpha = 0;
            
            
            UILabel *label = [view.subviews lastObject];
            label.alpha = 0;
            UIImageView *albumImageView = [view.subviews objectAtIndex:3];
            albumImageView.alpha = 0;
            
            Singleton *singleton = [Singleton shareManager];
            singleton.name = @"暂停";
            [singleton.audio pause];
            [button setImage:[UIImage imageNamed:@"play.ico"] forState:UIControlStateNormal];
            
        }
    }
    
}

#pragma mark - 创建推荐界面的tableView
- (void)createRecommendTableView
{
    self.recommendTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 667 * 200, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height / 667 * 64 - self.view.frame.size.height / 667 * 40  - self.view.frame.size.height / 667 * 70 - self.view.frame.size.height / 667 * 200)];
    self.recommendTableView.backgroundColor = [UIColor clearColor];
//    self.recommendTableView.backgroundColor= [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.3];
    
    self.recommendTableView.delegate = self;
    self.recommendTableView.dataSource = self;
    [self.view addSubview:self.recommendTableView];
    [self.recommendTableView registerClass:[RecommendTableViewCell class] forCellReuseIdentifier:@"cell"];
   
    [self.recommendTableView release];
}

#pragma mark - tableViewCell要显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    [cell.cellImageView setImage:[UIImage imageNamed:@"/Users/dlios/MaiMiaoMusicPlayer/MusicPlayer/MusicPlayer/MusicLibrary/Recommend/Controller/5.jpg"]];
    // 添加cell的样式
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.backgroundColor = [UIColor clearColor];
    NSDictionary *dic = [self.sectionArray objectAtIndex:indexPath.section];
    NSArray *smallArr = [dic objectForKey:@"data"];
    NSDictionary *smallDic = [smallArr objectAtIndex:indexPath.row];
    cell.titleLabel.text = [smallDic objectForKey:@"name"];
    cell.introLabel.text = [smallDic objectForKey:@"desc"];
    NSString *str = [smallDic objectForKey:@"pic_url"];
    NSURL *imageUrl = [NSURL URLWithString:str];
    [cell.cellImageView sd_setImageWithURL:imageUrl placeholderImage:nil];
//    NSString *str = model.pic_url;
//    NSURL *imageUrl = [NSURL URLWithString:str];
//    [cell.cellImageView sd_setImageWithURL:imageUrl placeholderImage:nil];
    return cell;
}

#pragma mark - 点击tableView后的效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 对点击的section进行判断
    if (indexPath.section == 0) {
        // 对点击的row进行判断
        if (indexPath.row == 0) {
           RecommendNewMusicViewController *newMusic = [[RecommendNewMusicViewController alloc] init];
            newMusic.array = @[@"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[97210245]", @"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[97210596]", @"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[97215317]"];
            [self.navigationController pushViewController:newMusic animated:YES];
        }
        
        if (indexPath.row == 1) {
            ReNewAlbumViewController *newAlbum = [[ReNewAlbumViewController alloc] init];
            [self.navigationController pushViewController:newAlbum animated:YES];
        }
    } else if (indexPath.section == 1) {
        ReHotSongListViewController *hot = [[ReHotSongListViewController alloc] init];
        NSArray *array = [self.sectionArray[1] objectForKey:@"data"];
        NSDictionary *dic = [array objectAtIndex:indexPath.row];
        NSDictionary *dic2 = [dic objectForKey:@"action"];
        NSString *str = [dic2 objectForKey:@"value"];
//        hot.str = @"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[97211543]";
        hot.str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[%@]", str];
        [self.navigationController pushViewController:hot animated:YES];
    } else if (indexPath.section == 2){
        ReHotSongListViewController *hot = [[ReHotSongListViewController alloc] init];
        NSArray *array = [self.sectionArray[2] objectForKey:@"data"];
        NSDictionary *dic = [array objectAtIndex:indexPath.row];
        NSDictionary *dic2 = [dic objectForKey:@"action"];
        NSString *str = [dic2 objectForKey:@"value"];

        hot.str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[%@]", str];
        [self.navigationController pushViewController:hot animated:YES];
    }
}

#pragma mark - 设置tableView的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.width / 375 * 90;
}

#pragma mark - tableView的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = [self.sectionArray objectAtIndex:section];
    NSArray *array = [dic objectForKey:@"data"];
    return array.count;
}

#pragma mark - section的数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionArray.count;
}

#pragma mark - 获取section的值
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    NSMutableDictionary *dic = [self.sectionArray objectAtIndex:section];
//    NSString *str = [dic objectForKey:@"name"];
//    
//    
//    
//    return str;
//}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* myView = [[[UIView alloc] init] autorelease];
    myView.backgroundColor = [UIColor clearColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 667 * 40)];
    titleLabel.backgroundColor = [UIColor lightGrayColor];
    titleLabel.alpha = 0.5;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 375 * 5, self.view.frame.size.height / 667 * 5, self.view.frame.size.width / 375 * 100, self.view.frame.size.height / 667 * 25)];
   
    NSMutableDictionary *dic = [self.sectionArray objectAtIndex:section];
    NSString *str = [dic objectForKey:@"name"];
    label.text= str;
   
    label.font = [UIFont systemFontOfSize:18];
    [myView addSubview:titleLabel];
    [myView addSubview:label];
    [titleLabel release];
    return myView;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return self.view.frame.size.height / 667 * 40;
}
			



@end
